{extend name="../../base/view/common/base" /}
<!-- 主体 -->
{block name="body"}
<style>
    .layui-laydate-header {
        display: none;
    }

    .layui-laydate-content > table > thead {
        display: none;
    }

    .laydate-day-prev {
        display: none;
    }

    .laydate-day-next {
        display: none;
    }

    .pl_btn {
        color: #4c9cfb !important;
        cursor: pointer;
    }
</style>
<form class="layui-form p-4">
    <h3 class="pb-3">功能菜单/节点</h3>
    <table class="layui-table layui-table-form">
        <tr>
            <td class="layui-td-gray-2">单号<font>*</font>
            </td>
            <td>
                <input type="text" name="orderNum" value="{$orderNum|default=''}" lay-verify="required" readonly
                       autocomplete="off" placeholder="请输入"
                       class="layui-input disabled">
            </td>
            <td class="layui-td-gray-2">{$pType>2?'供应商':'客户'}</td>
          <td>
            <select name="punterId"  id="punterId" lay-search lay-filter="punterId">
                <option value="">请选择</option>
                {volist name="punterList" id="pl"}
                <option value="{$pl.id}" {if isset($khId)&&$khId==$pl.id} selected {/if} >{$pl.name}</option>
                {/volist}
            </select>
        </td>
            <td class="layui-td-gray">单据日期<font>*</font></td>
            <td>
                <input type="text" name="orderDate" id="orderDate" value="{$detail.orderDate|default=date('Y-m-d')}" lay-verify="required" readonly
                       autocomplete="off" placeholder="选择日期"
                       class="layui-input">
            </td>
        </tr>
        {if $pType=='1'}
        <tr>

            <td class="layui-td-gray">收货人
            </td>
            <td>
                <select name="saveId" id="saveId" lay-search lay-filter="saveId">
                    <option value="">请选择</option>
                </select>
            </td>
            <td class="layui-td-gray-2">收货人电话

            </td>
            <td> <input type="text" name="savePhone" value="{$detail.savePhone|default=''}"  readonly class="layui-input layui-disabled" placeholder="选择收货人">
                <input type="hidden" name="saveName" value="{$detail.saveName|default=''}">
            </td>
            <td class="layui-td-gray-2">收货人地址
            </td>
            <td>
                <input type="text" name="saveAdd" value="{$detail.saveAdd|default=''}"  readonly class="layui-input layui-disabled" placeholder="选择收货人">

            </td>
        </tr>
        {/if}
{if $pType=='1'}
        <tr>
            <td class="layui-td-gray-2">快递公司
            </td>
            <td>
                <input type="text" name="kdCompany" value=""
                       autocomplete="off" placeholder="请输入"
                       class="layui-input ">
            </td>
            <td class="layui-td-gray-2">快递单号
            </td>
            <td>
                <input type="text" name="kdOrderNum" value=""
                       autocomplete="off" placeholder="请输入"
                       class="layui-input ">
            </td>
        </tr>
{/if}
        <tr>
            <td colspan="6"><h4>明细</h4></td>
        </tr>
        {if $pType==1}
        <tr>

            <td colspan="6">

                <table class="layui-table tableMany">
                    <thead>
                    <tr>
                        <td>序号</td>
                        <td>品牌</td>
                        <td>品名</td>
                        <td>型号</td>
                        <td>订货号</td>
                        <td>单位</td>
                        <td>库存数量</td>
                        <td>锁定数量</td>
                        <td>出库数量</td>
                        <td>关联采购合同</td>
                        <td>关联销售合同</td>
                    </tr>
                    </thead>
                    <tbody id="mxTbdy">
                    {volist name="list" id="ml" key='k'}
                    <tr>
                        <td>
                            <span class="xuhao">{$k}</span>
                            <input type="hidden" name="mx[mxid][]" value="{$ml.detailId}">
                            <input type="hidden" name="mx[lockedId][]" value="{$ml.id}">
                            <input type="hidden" class="mx_mdId" name="mx[mdId][]" value="{$ml.mdId}">
                            <input type="hidden" class="" name="mx[sodPrice][]" value="{$ml.sodPrice}">
                            <input type="hidden" class="mx_buyContactMxId" name="mx[buyContactMxId][]" value="{$ml.buyContactMxId}">
                        </td>
                        <td>{$ml.brand}</td>
                        <td>{$ml.mdName}</td>
                        <td>{$ml.specs}</td>
                        <td>{$ml.buyCode}</td>
                        <td>{$ml.unit}</td>
                        <td><input type="text" name="mx[kcNum][]" readonly value="{$ml.inNum}" class="layui-input layui-disabled ckNum"
                                   lay-verify="required" oninput="digitalFiltering(this,2)"></td>
                        <td><input type="text" name="mx[occupyNum][]" class="layui-input occupyNum layui-disabled" value="{$ml.hasLock}" readonly></td>
                        <td><input type="text" name="mx[num][]" value="{$ml.hasLock}" class="layui-input num"
                                   lay-verify="required" oninput="digitalFiltering(this,2)" onblur="checkNums(this)"></td>
                        <td><input type="text" class="layui-input layui-disabled" readonly name="mx[saleContractOrder][]" value="{$ml.saleContactOrder}"></td>
                        <td><input type="text" class="layui-input layui-disabled" readonly name="mx[buyContactOrder][]" value="{$ml.buyContactOrder}"></td>
                    </tr>
                    {/volist}
                    </tbody>
                </table>

            </td>
        </tr>
        {else}
        <tr>

            <td colspan="6">

                <table class="layui-table tableMany">
                    <thead>
                    <tr>
                        <td>序号</td>
                        <td>物料编码</td>
                        <td>品牌</td>
                        <td>品名</td>
                        <td>型号</td>
                        <td>订货号</td>
                        <td>计量单位</td>
                        <td>仓库</td>
                        <td>数量</td>
                    </tr>
                    </thead>
                    <tbody id="mxTbdy">
                    {volist name="list" id="ml" key='k'}
                    <tr>
                        <td>
                            <span class="xuhao">{$k}</span>
                            <input type="hidden" name="mx[saleContractOrder][]" value="{$ml.orderNum}">
                            <input type="hidden" name="mx[mxid][]" value="{$ml.id}">
                            <input type="hidden" class="mx_mdId" name="mx[mdId][]" value="{$ml.mdId}">
                            <input type="hidden" class="" name="mx[sodPrice][]" value="{$ml.sodPrice}">
                            <input type="hidden" class="" name="mx[thisId][]" value="{$ml.outId}">
                            <input type="hidden" class="" name="mx[outThisId][]" value="{$ml.rkId}">
                        </td>
                        <td>{$ml.bar_code}</td>
                        <td>{$ml.brand}</td>
                        <td>{$ml.mdName}</td>
                        <td>{$ml.specs}</td>
                        <td>{$ml.buyCode}</td>
                        <td>{$ml.unit}</td>
                        <td>
                            <select name="mx[wId][]" >
                                {volist name="warehouse" id="wl"}
                                <option value="{$wl.id}" >{$wl.name}</option>
                                {/volist}
                            </select>
                        </td>
                      <td><input type="text" name="mx[num][]" value="{$ml.bjNum}" class="layui-input num"
                                   lay-verify="required" data-oldNum="{$ml.bjNum}" oninput="digitalFiltering(this,2)" onblur="chekNum(this)"></td>
                    </tr>
                    {/volist}
                    </tbody>
                </table>

            </td>
        </tr>
        {/if}
    </table>
    <div class="py-3">
        <input type="hidden" name="ids" value="{$ids}">
        <button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="webform">保存</button>
    </div>
</form>
{/block}
<!-- /主体 -->

<!-- 脚本 -->
{block name="script"}
<script>
    const moduleInit = ['tool'];
    let khId="{$khId|default=0}"
    function gouguInit() {
        var form = layui.form, tool = layui.tool, level = 0, laydate = layui.laydate;
        laydate.render({
            elem:'#orderDate'
        })
        //监听提交
        form.on('submit(webform)', function (data) {
            let callback = function (e) {
                layer.msg(e.msg);
                if (e.code == 0) {
                    setTimeout(function () {
                        parent.parent.location.reload();
                    }, 1000);
                }
            }
            tool.post("{:url('')}", data.field, callback);
            return false;
        });
        form.on('select(punterId)', function (data) {
            getPunterMember(data.value);
        });

        form.on('select(saveId)', function (data) {
            let value=data.value;
            if (data.value==''){
                $("input[name ='saveName']").val();
                $("input[name ='savePhone']").val();
                $("input[name ='saveAdd']").val();
            }else {
                saveList.forEach(function (item) {
                    if (item.id==value){
                        $("input[name ='saveName']").val(item.name);
                        $("input[name ='savePhone']").val(item.phone);
                        $("input[name ='saveAdd']").val(item.address);
                        return;
                    }
                })
            }
        });
       if (khId>0){
           getPunterMember(khId)
       }
    }
    function getPunterMember(punterId='') {
        $("#contactId").empty();
        $("#saveId").empty();
        $("input[name ='contactName']").val();
        $("input[name ='contactPhone']").val();
        $("input[name ='saveName']").val();
        $("input[name ='savePhone']").val();
        $("input[name ='saveAdd']").val();
        contactList=[];
        saveList=[];
        if (punterId==''){
            layui.form.render();
            return;
        }
        httpRequest("{:url('api/getPurveyorMember')}",{purveyorId:punterId},function (res) {
            if (res.code==0){
                contactList=res.data.contactList;
                saveList=res.data.saverList;
                let contactStr='  <option value="">请选择</option>';
                let saverStr='  <option value="">请选择</option>';
                let contactId="{$detail.contactId|default='0'}"
                let saveId="{$detail.saveId|default='0'}"
                contactList.forEach(function (item) {
                    let cStr='';
                    if (item.id==contactId){
                        cStr='selected';
                    }
                    contactStr+='  <option value="'+item.id+'" '+cStr+'>'+item.name+'</option>'
                })
                saveList.forEach(function (item) {
                    let s_Str='';
                    if (item.id==saveId){
                        s_Str='selected';
                    }
                    saverStr+='  <option value="'+item.id+'" '+s_Str+'>'+item.name+'</option>'
                })
                console.log(saverStr)
                $("#contactId").append(contactStr);
                $("#saveId").append(saverStr);
                layui.form.render();
            }else {

            }
        },false)
    }
    function checkNums(ojb) {
        let trObj = $(ojb).parent().parent();
        let occupyNum = trObj.find(".occupyNum").val()
        let ckNum=trObj.find(".ckNum").val()
        if (occupyNum==''){
            occupyNum=0;
        }
        console.log('aaaa',parseFloat(occupyNum))
        let num = trObj.find(".num").val()
        if (parseFloat(num)>parseFloat(occupyNum)){
            layer.msg('出库数量不能大于占用库存')
            trObj.find(".num").val('')
            return
        }
        if (parseFloat(num)>parseFloat(ckNum)){
            layer.msg('出库数量不能大于库存')
            trObj.find(".num").val('')
            return;
        }
    }
    function chekNum(obj) {
        let val=$(obj).val();
        let oldVal=$(obj).attr('data-oldNum')
        if (parseFloat(val)>parseFloat(oldVal)){
            layer.msg('数量大于可退数量');
            $(obj).val('');
            return
        }
    }
</script>
{/block}
<!-- /脚本 -->